/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import dao.forgotPassDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

/**
 *
 * @author sondv_b01229
 */
@WebServlet(name = "forgotPassServlet", urlPatterns = {"/forgotPassServlet"})
public class forgotPassServlet extends HttpServlet {
    forgotPassDao fd=new forgotPassDao();
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            request.getRequestDispatcher("user/ForgotPassword.jsp").forward(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String email=request.getParameter("email");
        String pass=fd.findPass(email);
        if(pass!=null){
            String host = "smtp.gmail.com";
            Properties props  = System.getProperties();
            props.put("mail.smtps.auth", "true");
            props.put("mail.smtps.starttls.enable", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.user", "hungk4ht@gmail.com");
            props.put("mail.smtp.password", "chabaya2310");
            props.put("mail.smtp.port", "587");
            Session session = Session.getDefaultInstance(props,null);
            MimeMessage message = new MimeMessage(session);
            try {
                message.setFrom(new InternetAddress("hungk4ht@gmail.com"));
                message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
                message.setSubject("Pass");
                message.setText(pass);
                Transport transport = session.getTransport("smtps");
                transport.connect(host,"hungk4ht@gmail.com", "chabaya2310");
                transport.sendMessage(message, message.getAllRecipients());
                transport.close();
                
            } catch (Exception ex) {
                
            }
            String tb="Password has been sent to the email";
            request.setAttribute("tb", tb);
            request.getRequestDispatcher("user/ForgotPassword.jsp").forward(request, response);
        }else{
            String tb="Email does not exist";
            request.setAttribute("tb", tb);
            request.getRequestDispatcher("user/ForgotPassword.jsp").forward(request, response);
        }
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
